<?php

class Line extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Mline');
    }

    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'line/index_view';
        
        if ($this->input->post('yt0')) {
            $line = $this->input->post('Line');
            //Form validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Line[code]', 'Name', 'required');
            if ($this->form_validation->run()) {                
                //Check and Upload File
                $config['upload_path'] = './uploads/lines/';
        		$config['allowed_types'] = 'gif|jpg|png|svg';
        		$config['max_size']	= '10240';      
        		$this->load->library('upload', $config);
        
        		if ( ! $this->upload->do_upload('fUpload'))
        		{
        			$this->_data['data']['error'] = $this->upload->display_errors();
        		}
        		else
        		{  		           
        			$upload_data = $this->upload->data();
                    $line['map'] = $upload_data['file_name'];     
                    $this->Mline->insertLine($line);
                    $this->session->set_flashdata('flash_mess', 'Line has been created successfully.'); 
                    redirect(base_url('line/manage'));                    
        		}
            } else {
                $this->_data['data']['error'] = validation_errors('<li>', '</li>');
            }
        }
        //Load View
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function display($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'line/display_view';
        //Pagination   
        $config['base_url'] = base_url('line/display/');
        $config['total_rows'] = $this->Mline->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();        
        //Load View
        $this->_data['data']['line'] = $this->Mline->listAllLines($config['per_page'], $page, 'id desc');
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function manage($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'line/manage_view'; 
        //Pagination   
        $config['base_url'] = base_url('line/manage/');
        $config['total_rows'] = $this->Mline->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        //echo '<pre>';
        //print_r($line);echo'</pre>';exit();
        $this->_data['current_page'] = $page;
        $this->_data['data']['line'] = $this->Mline->listAllLines($config['per_page'], $page, 'id desc');  
        $this->_data['flash_mess'] = $this->session->flashdata('flash_mess');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function delete($id) {
        $this->Mline->deleteLine($id);
        redirect(base_url('line/manage'));
    }
    
    public function update($id) {
        //Pre-load View
        $this->_data['load_page'] = 'line/update_view';
        $this->_data['data'] = $this->Mline->getLineById($id);
        $this->_data['data_id'] = $id;
        //Process        
        if ($this->input->post('yt0')) {
            $line = $this->input->post('Line');
            //Form validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Line[code]', 'Name', 'required');
            if ($this->form_validation->run()) {                
                //Check and Upload File
                if ($this->input->post('oldImg') != $_FILES['fUpload']['name']) {
                    echo "<pre>";print_r($_FILES['fUpload']);echo "</pre>";
                    $config['upload_path'] = './uploads/lines/';
            		$config['allowed_types'] = 'gif|jpg|png|svg';
            		$config['max_size']	= '10240';      
            		$this->load->library('upload', $config);
                    //Uploading
            		if ( ! $this->upload->do_upload('fUpload')) {
            		  $this->_data['data']['error'] = $this->upload->display_errors();
            		} else {
            		  $upload_data = $this->upload->data();
            		  $line['map'] = $upload_data['file_name'];  
            		}
                } 		           
                if (empty($this->_data['data']['error'])) {
                    $this->Mline->updateLine($line, $id);
                    $this->session->set_flashdata('flash_mess', 'Line has been updated successfully.'); 
                    redirect(base_url('line/manage'));  
                }            
        		
            } else {
                $this->_data['data']['error'] = validation_errors('<li>', '</li>');
            }
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);       
    }
    
    public function view($id) {
        //Pre-load View
        $this->_data['load_page'] = 'line/view_view';
        $this->load->model('Mstation');
        $this->load->model('Mvehicle');
        //Pagination   
        
        //Load View
        $this->_data['data'] = $this->Mline->getLineById($id);
        $this->_data['data_id'] = $id;
        $this->_data['station'] = $this->Mstation->listStationByLineId($id);
        $this->_data['vehicle'] = $this->Mvehicle->listVehicleByLineId($id);
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function inter($id, $sid="") {
        //Pre-load View
        $this->_data['load_page'] = 'line/inter_view';
        $this->load->model('Mstation');
        $this->_data['data'] = $this->Mline->getLineById($id);
        $this->_data['station_line'] = $this->Mstation->listStationByLineId($id);
        //Process    
        $station_id = $this->input->post('sltStation');    
        if ($this->input->post('yt0')) {
            if (count($this->_data['station_line']) == 7) {
                $this->_data['data']['error'] = 'Exceeded Maximum allowed Station.';
            } else {
                $this->Mstation->setStationInter($id, $station_id);
                redirect(base_url('line/inter/'.$id));
            }         
        }
        if ($sid != "") {
            $this->Mstation->deleteInter($sid);
            redirect(base_url('line/inter/'.$id));
        }
        //Load View        
        $this->_data['data_id'] = $id;
        $this->_data['station'] = $this->Mstation->listAllStations();        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function startend($id) {
        //Pre-load View
        $this->_data['load_page'] = 'line/startend_view';
        $this->load->model('Mstation');
        //Process    
        $station_id = $this->input->post('sltStation');    
        if ($this->input->post('yt0')) {
            $station_id = $this->input->post('sid');
            $position = $this->input->post('startend');
            //Checking START-END selected
            $start_id = 0;
            $end_id = 0;
            if ($position) {
                foreach ($position as $key => $item) {
                    if ($item == 'START') {
                        $start_id = $station_id[$key];
                    } elseif ($item == 'END') {
                        $end_id = $station_id[$key];
                    }
                }
            }
            if ($start_id != 0) {
                $this->Mstation->deletePositionByLineId('START',$id);
            }
            if ($end_id != 0) {
                $this->Mstation->deletePositionByLineId('END',$id);
            }
            
            //Update START
            $this->Mstation->updateStation(array('position_station'=> 'START'), $start_id);
            //Update END
            $this->Mstation->updateStation(array('position_station'=> 'END'), $end_id);
            
        }
        //Load View
        $this->_data['data'] = $this->Mline->getLineById($id);
        $this->_data['data_id'] = $id;
        $this->_data['station'] = $this->Mstation->listStationByLineId($id);        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function vehicle($id, $sid="") {
        //Pre-load View
        $this->_data['load_page'] = 'line/vehicle_view';
        $this->load->model('Mvehicle');
        $this->_data['data'] = $this->Mline->getLineById($id);
        $this->_data['vehicle_line'] = $this->Mvehicle->listVehicleByLineId($id);
        //Process    
        $vehicle_id = $this->input->post('sltVehicle');    
        if ($this->input->post('yt0')) {
            if (count($this->_data['vehicle_line']) == 10) {
                $this->_data['data']['error'] = 'Exceeded Maximum allowed Vehicle.';
            } else {
                $this->Mvehicle->setVehicle($id, $vehicle_id);
                redirect(base_url('line/vehicle/'.$id));
            }         
        }
        if ($sid != "") {
            $this->Mvehicle->deleteSetVehicle($sid);
            redirect(base_url('line/vehicle/'.$id));
        }
        //Load View        
        $this->_data['data_id'] = $id;
        $this->_data['vehicle'] = $this->Mvehicle->listAllVehicles();        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function check_name_exist() {
        $input = $this->input->post("Line");
        if ($this->Mline->getLineByName($input['code']) != 0) {
            $this->form_validation->set_message('check_name_exist', 'Name has been created in system. Please choose another');
            return false;
        } else {
            return true;
        }
    }

}

?>